1100 #include "asm.h"
1101 
1102 # Each non-boot CPU ("AP") is started up in response to a STARTUP
1103 # IPI from the boot CPU.  Section B.4.2 of the Multi-Processor
1104 # Specification says that the AP will start in real mode with CS:IP
1105 # set to XY00:0000, where XY is an 8-bit value sent with the
1106 # STARTUP. Thus this code must start at a 4096-byte boundary.
1107 #
1108 # Because this code sets DS to zero, it must sit
1109 # at an address in the low 2^16 bytes.
1110 #
1111 # Bootothers (in main.c) sends the STARTUPs, one at a time.
1112 # It puts this code (start) at 0x7000.
1113 # It puts the correct %esp in start-4,
1114 # and the place to jump to in start-8.
1115 #
1116 # This code is identical to bootasm.S except:
1117 #   - it does not need to enable A20
1118 #   - it uses the address at start-4 for the %esp
1119 #   - it jumps to the address at start-8 instead of calling bootmain
1120 
1121 #define SEG_KCODE 1  // kernel code
1122 #define SEG_KDATA 2  // kernel data+stack
1123 
1124 #define CR0_PE    1  // protected mode enable bit
1125 
1126 .code16                       # Assemble for 16-bit mode
1127 .globl start
1128 start:
1129   cli                         # Disable interrupts
1130 
1131   # Set up the important data segment registers (DS, ES, SS).
1132   xorw    %ax,%ax             # Segment number zero
1133   movw    %ax,%ds             # -> Data Segment
1134   movw    %ax,%es             # -> Extra Segment
1135   movw    %ax,%ss             # -> Stack Segment
1136 
1137   # Switch from real to protected mode, using a bootstrap GDT
1138   # and segment translation that makes virtual addresses
1139   # identical to physical addresses, so that the
1140   # effective memory map does not change during the switch.
1141   lgdt    gdtdesc
1142   movl    %cr0, %eax
1143   orl     $CR0_PE, %eax
1144   movl    %eax, %cr0
1145 
1146 
1147 
1148 
1149 
1150   # This ljmp is how you load the CS (Code Segment) register.
1151   # SEG_ASM produces segment descriptors with the 32-bit mode
1152   # flag set (the D flag), so addresses and word operands will
1153   # default to 32 bits after this jump.
1154   ljmp    $(SEG_KCODE<<3), $start32
1155 
1156 .code32                       # Assemble for 32-bit mode
1157 start32:
1158   # Set up the protected-mode data segment registers
1159   movw    $(SEG_KDATA<<3), %ax    # Our data segment selector
1160   movw    %ax, %ds                # -> DS: Data Segment
1161   movw    %ax, %es                # -> ES: Extra Segment
1162   movw    %ax, %ss                # -> SS: Stack Segment
1163   movw    $0, %ax                 # Zero segments not ready for use
1164   movw    %ax, %fs                # -> FS
1165   movw    %ax, %gs                # -> GS
1166 
1167   # Set up the stack pointer and call into C.
1168   movl    start-4, %esp
1169   call	*(start-8)
1170 
1171   # If the call returns (it shouldn't), trigger a Bochs
1172   # breakpoint if running under Bochs, then loop.
1173   movw    $0x8a00, %ax            # 0x8a00 -> port 0x8a00
1174   movw    %ax, %dx
1175   outw    %ax, %dx
1176   movw    $0x8ae0, %ax            # 0x8ae0 -> port 0x8a00
1177   outw    %ax, %dx
1178 spin:
1179   jmp     spin
1180 
1181 # Bootstrap GDT
1182 .p2align 2                                # force 4 byte alignment
1183 gdt:
1184   SEG_NULLASM                             # null seg
1185   SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff)   # code seg
1186   SEG_ASM(STA_W, 0x0, 0xffffffff)         # data seg
1187 
1188 gdtdesc:
1189   .word   (gdtdesc - gdt - 1)                            # sizeof(gdt) - 1
1190   .long   gdt                             # address gdt
1191 
1192 
1193 
1194 
1195 
1196 
1197 
1198 
1199 
